package top.wardyan.wbaseswt.listener;

import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;

/**
 * 只支持数字, 不包括任何标点符号
 * @author ward
 */
public class WNumberModifyAdapter implements ModifyListener{

	@Override
	public void modifyText(ModifyEvent e) {
		Text source = (Text)e.getSource();
		source.removeModifyListener(new WNumberModifyAdapter());
		String text = source.getText();
		for(char c : source.getText().toCharArray()) {
			if(c < 48 || c > 57) {
				text = text.replace(c, (char) 0);
			}
		}
		source.setText(text);
		source.addModifyListener(new WNumberModifyAdapter());
	}
}
